namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorDirectionalMovementIndex : BaseIndicator
    {
        private string dmDownField = ("DMI.DmDownField" + Common.GetGuid());
        private string dmDownMaField = ("DMI.DmDownMaField" + Common.GetGuid());
        private string dmUpField = ("DMI.DmUpField." + Common.GetGuid());
        private string dmUpMaField = ("DMI.DmUpMaField" + Common.GetGuid());
        private string dxField = ("DMI.DxField." + Common.GetGuid());
        private LineShape tlsAdx;
        private LineShape tlsAdxr;
        private LineShape tlsMdi;
        private LineShape tlsPdi;
        private string trField = ("DMI.TrField." + Common.GetGuid());
        private string trMaField = ("DMI.TrMaField" + Common.GetGuid());

        public IndicatorDirectionalMovementIndex()
        {
            base.IndType = IndicatorType.DMI;
            base.IndName = "DMI";
            base.IndDesc = "趋向指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 14.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 6.0));
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "PDI", "MDI", "ADX", "ADXR" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.TlsPdi) && (obj != this.TlsMdi)) && ((obj != this.TlsAdx) && (obj != this.TlsAdxr)))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double curHigh = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double curLow = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double curValue = curHigh;
            if (r > 0)
            {
                curValue -= Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["High"]]);
            }
            if (curValue < 0.0)
            {
                curValue = 0.0;
            }
            double num4 = -curLow;
            if (r > 0)
            {
                num4 += Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Low"]]);
            }
            if (num4 < 0.0)
            {
                num4 = 0.0;
            }
            if (curValue > num4)
            {
                num4 = 0.0;
            }
            else if (curValue < num4)
            {
                curValue = 0.0;
            }
            double num5 = Common.CalculateTrueRange(r, curHigh, curLow, base.SourceDic["High"], base.SourceDic["Low"], base.Crab.DataSource);
            double num6 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.trMaField, this.trField, num5, base.Crab.DataSource);
            double num7 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.dmUpMaField, this.dmUpField, curValue, base.Crab.DataSource);
            double num8 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.dmDownMaField, this.dmDownField, num4, base.Crab.DataSource);
            double num9 = 0.0;
            double num10 = 0.0;
            if (num6 != 0.0)
            {
                num9 = (num7 / num6) * 100.0;
                num10 = (num8 / num6) * 100.0;
            }
            double num11 = Math.Abs((double) (num9 - num10));
            double num12 = num9 + num10;
            double num13 = 0.0;
            if (num12 != 0.0)
            {
                num13 = (num11 / num12) * 100.0;
            }
            double num14 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsAdx.Name, this.dxField, num13, base.Crab.DataSource);
            double num15 = num14;
            if (r > 0)
            {
                num15 += Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][this.tlsAdx.Name]);
            }
            num15 /= 2.0;
            return new double[] { num9, num10, curValue, num4, num7, num8, num5, num6, num13, num14, num15 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.TlsPdi, this.TlsMdi, this.TlsAdx, this.TlsAdxr };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsPdi);
            base.Crab.DeleteShape(this.TlsMdi);
            base.Crab.DeleteShape(this.TlsAdx);
            base.Crab.DeleteShape(this.TlsAdxr);
            base.Crab.DataSource.Columns.Remove(this.TrField);
            base.Crab.DataSource.Columns.Remove(this.TrMaField);
            base.Crab.DataSource.Columns.Remove(this.DmDownField);
            base.Crab.DataSource.Columns.Remove(this.DmDownMaField);
            base.Crab.DataSource.Columns.Remove(this.DmUpField);
            base.Crab.DataSource.Columns.Remove(this.DmUpMaField);
            base.Crab.DataSource.Columns.Remove(this.DxField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsPdi.Name, numArray[0], r);
                        base.Crab.SetValue(this.TlsMdi.Name, numArray[1], r);
                        base.Crab.SetValue(this.DmUpField, numArray[2], r);
                        base.Crab.SetValue(this.DmDownField, numArray[3], r);
                        base.Crab.SetValue(this.DmUpMaField, numArray[4], r);
                        base.Crab.SetValue(this.DmDownMaField, numArray[5], r);
                        base.Crab.SetValue(this.TrField, numArray[6], r);
                        base.Crab.SetValue(this.TrMaField, numArray[7], r);
                        base.Crab.SetValue(this.DxField, numArray[8], r);
                        base.Crab.SetValue(this.TlsAdx.Name, numArray[9], r);
                        base.Crab.SetValue(this.TlsAdxr.Name, numArray[10], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsPdi = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMdi = base.Crab.AddLine(fields[1], panelID);
            this.TlsAdx = base.Crab.AddLine(fields[2], panelID);
            base.Crab.SetLine(fields[2], fields[2], Color.FromArgb(0xff, 0, 0xff), 1, 0.01, 2, DashStyle.Solid);
            this.TlsAdxr = base.Crab.AddLine(fields[3], panelID);
            base.Crab.SetLine(fields[3], fields[3], Color.Lime, 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["High"] = source[0];
            base.SourceDic["Low"] = source[1];
            base.SourceDic["Close"] = source[2];
            base.IndPeriod[0].ParamValue = periods[0];
            base.Crab.DataSource.Columns.Add(this.TrField);
            base.Crab.DataSource.Columns.Add(this.TrMaField);
            base.Crab.DataSource.Columns.Add(this.DmDownField);
            base.Crab.DataSource.Columns.Add(this.DmDownMaField);
            base.Crab.DataSource.Columns.Add(this.DmUpField);
            base.Crab.DataSource.Columns.Add(this.DmUpMaField);
            base.Crab.DataSource.Columns.Add(this.DxField);
            base.IndPeriod[1].ParamValue = periods[1];
        }

        public string DmDownField
        {
            get
            {
                return this.dmDownField;
            }
            set
            {
                this.dmDownField = value;
            }
        }

        public string DmDownMaField
        {
            get
            {
                return this.dmDownMaField;
            }
            set
            {
                this.dmDownMaField = value;
            }
        }

        public string DmUpField
        {
            get
            {
                return this.dmUpField;
            }
            set
            {
                this.dmUpField = value;
            }
        }

        public string DmUpMaField
        {
            get
            {
                return this.dmUpMaField;
            }
            set
            {
                this.dmUpMaField = value;
            }
        }

        public string DxField
        {
            get
            {
                return this.dxField;
            }
            set
            {
                this.dxField = value;
            }
        }

        public LineShape TlsAdx
        {
            get
            {
                return this.tlsAdx;
            }
            set
            {
                this.tlsAdx = value;
            }
        }

        public LineShape TlsAdxr
        {
            get
            {
                return this.tlsAdxr;
            }
            set
            {
                this.tlsAdxr = value;
            }
        }

        public LineShape TlsMdi
        {
            get
            {
                return this.tlsMdi;
            }
            set
            {
                this.tlsMdi = value;
            }
        }

        public LineShape TlsPdi
        {
            get
            {
                return this.tlsPdi;
            }
            set
            {
                this.tlsPdi = value;
            }
        }

        public string TrField
        {
            get
            {
                return this.trField;
            }
            set
            {
                this.trField = value;
            }
        }

        public string TrMaField
        {
            get
            {
                return this.trMaField;
            }
            set
            {
                this.trMaField = value;
            }
        }
    }
}

